/*****************************************************************************/
/**
*
* @file wm8978.c
*
* User defined functions about wm8978.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  05/20/2019 Created
* </pre>
*
*------------------------------------------------------------------------------
* Author: abu	E-mail:	abu_liu@aliyun.com
*				Blog:	https://blog.csdn.net/ClamerKorallen
*				Gitee:	https://gitee.com/abu_liu
*------------------------------------------------------------------------------
*
******************************************************************************/
#include "wm8978.h"

/*****************************************************************************/
/**
*
* Write byte or bytes to wm8978 from specific register
*
* @param	reg_addr is the register offset to be written to.
*			value is the starting pointer to the datas which will be written to wm8978.
*			bytecnt is the number of datas that will be written
*
* @return	TRUE: function completed successfully.
*			FALSE:	datas was not written to wm8978.
*
* @note		None.
*
******************************************************************************/

s32 wm8978_write_bytes(u8 reg_addr, u8 *value, u8 bytecnt)
{
	s32 status;
	u8 data[bytecnt+1]; //the data that will be 
	u8 data_ptr;
	u32 i;
	
	if(XIicPs_BusIsBusy(WM8978_CPU_IIC_PTR) == TRUE)
	{

		xil_printf("IIC0 bus is busy\r\n");
		status = FALSE;
		return status;
	}

	//Ensure the iic device of cpu is in correct mode
	/*
	
	Leave this segment of code blank, for there's no need to do this at this moment.
	When there is some iic device else with different protocol on the same iic bus
	as wm8978, supplement this segment. ----by abu_liu, 05.31,2019
	
	*/

	data[0U] = reg_addr;
	data_ptr = &data[0U];

	//copy datas which will be written to wm8978 to a new array
	for(i=1U;i<=bytecnt;i++)
	{
		data[i] = *value;
		value ++;
	}


	//set interrupt handler for master-send mode
	XIicPs_SetStatusHandler(WM8978_CPU_IIC_PTR, WM8978_CPU_IIC_PTR, wm8978_intr_handler);
	
	XIicPs_MasterSend(WM8978_CPU_IIC_PTR, data_ptr, bytecnt, WM8978_BASEADDR);

	status = TRUE;
	return status;
}

/*****************************************************************************/
/**
*
* Wm8978 initialization function
* Set wm8978 to a fixed working mode. There is no need to change the register 
* values when wm8978 is already working.
*
* @param	None.
*
* @return	None.
*
*
* @note		None.
*
******************************************************************************/

void wm8978_init()
{
	while(wm8978_write_bytes(0U, wm8978_regval_table, wm8978_REG_NUM << 1U) != TRUE);
}

XIicPs_IntrHandler wm8978_intr_handler()
{

}



